#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/232.h"

/*
* this function is for set up the rs232 protocol
* parameters
*          baudrate-------> use the defined values located in the define.h
*          flags----------> configuration for the rs232
* does not return 
* NOTE --
*/
void setup_232(word baudrate, char flags){
  //baudrate = bsclk / (16 * SCIxBDH, SCIxBDL)
  SCI2BD = baudrate;
  SCI2C1 = flags;
  SCI2C2 = 0x0C;//enable the rx and tx
  SCI2C2_RIE = 1;// Receiver Interrupt Enable
}
/*
* send_232: this function send out data to RS232 protocol
* parameter:
*     *dato -----> pointer to data to be shiped
*
*     return:
*      0x01 -----> when the chipping buffer doesn't empty
*      0x00 -----> when success  
*/
char send_232(char *dato){
  char rta_fc = 0;
  int x = 0;
  while(*dato != 0){
    x = 0;
    while(!SCI2S1_TDRE && x < 1000){
      __RESET_WATCHDOG();
      x++;
    }
    if(x >= 1000) rta_fc = 0x01;
    __RESET_WATCHDOG();
    SCI2D = *dato++;
  }
  return rta_fc;
}